class Solution {
	//可能引起死循环的做法：当输入为负数时
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        while(n){
            if(n & 1)  ++count;  //若最后一位是1，则计数+1
            n = n >> 1;  //最左边补0
        }
        return count;
    }
};

class Solution {
    //常规解法：不移动n，将1依次左移
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        unsigned int flag = 1;
        while(flag){
            if(n & flag)  ++count;  //若最后一位是1，则计数+1
            flag = flag << 1;  //最左边补0
        }
        return count;
    }
};

class Solution {
    //高效解法：(n - 1) & n是将n的最右边的1变0
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        while(n){
            n = (n - 1) & n;
            ++count;
        }
        return count;
    }
};